﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGXEngine
{
    public class Command_CreateDataType_Var : ICommand
    {
        #region ICommand Members

        public bool IsReturn { get { return false; } }
        public bool IsIf { get { return false; } }

        public List<ICommand> ChildrenCommands
        {
            get { return _childrenCommands; }
            set { _childrenCommands = value; }
        }
        private List<ICommand> _childrenCommands = new List<ICommand>();

        public string Name { get; set; }

        public IVariableType Execute(ScriptManager sm, FunctionScope scope)
        {
            var arg = from a in scope.Arguments
                      where a.Name.ToLower().Trim() == this.Name
                      select a;

            if (arg.Count() > 0)
                return arg.Single();

            var va = from v in scope.Variables
                     where v.Name.ToLower().Trim() == this.Name
                     select v;

            if (va.Count() > 0)
                return va.Single();

            VarVariableType vt = new VarVariableType(this.Name);
            scope.Variables.Add(vt);

            foreach (var c in this.ChildrenCommands)
            {
                c.Execute(sm, scope);
            }

            return vt;
        }

        #endregion

        #region Public Methods

        public Command_CreateDataType_Var(string name)
        {
            this.Name = name.ToLower().Trim();
        }

        #endregion
    }
}
